home *** CD-ROM | disk | FTP | other *** search
- Path: prodigy.com!usenet
- From: Victor Muslin <vmuslin@prodigy.com>
- Newsgroups: comp.lang.c++
- Subject: Templates Question
- Date: Thu, 14 Mar 1996 16:35:26 -0500
- Organization: Prodigy Services Company
- Message-ID: <3148911E.1E02@prodigy.com>
- NNTP-Posting-Host: 192.203.241.84
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (WinNT; I)
-
- I want to define a class, let's call it "River", whose interface
- is similar to an IOStream. That is, I would like to be able to do:
-
- main()
- {
- River r;
- int i = 10;
-
- r << i << "foo";
- }
-
- My first impulse was to do:
-
- class River
- {
- public:
- River& operator<<(int);
- River& operator<<(char*);
- ...
- };
-
- However, redefining operator << for every built in data type
- is not fun and I would like to avoid long complex macros.
-
- So I thought that templates would be perfect:
-
- template <class T>
- class River<T>
- {
- public:
- River<T>& operator<<(T);
- ...
- };
-
- The problem with this seems to be that when I instansiate a
- River object:
-
- River<int> r;
-
- the template generates only:
-
- River& operator<<(int);
-
- function, which means that I can do:
-
- r << i;
-
- but not
-
- r << "foo";
-
- Is this correct and if so, any suggestions on how to avoid
- using macros or redefining << by hand for every data type?
-
- Please send a copy of replies to vmuslin@prodigy.com.
-
- Thanks.
-